<?php

defined('IN_MAIN') or exit('Invalid Access!');

    

function build_matrix_network_name($hid, $enable_radio, $row=0, $highlight_uid="", $selected_slot="", $css_prefix="admin"){

/*

This function build the matrix network for the input hid (head id), with the matrix head being the hid.

if enable radio is true, then it will output the empty box (box without downline user) with a radio button.

if input row is not zero, then it will start to display the matrix network from the input "row", and this also depends on the present of the

querystring "row", if querystring "row" present, then it will display that particular row according to the querystring (highest priority).

the highlight_uid is it will highlight the slot if that slot user id is same with this highlight_uid. if it is the 1st time of building

this matrix network, and highlight_uid is a direct matrix downline of hid, then it will search for the highlight_uid and display the matrix

network in such that the highlight_uid is visible on the 1st time build, and will navigate to appropriate row for that.

*/



global $vars, $get_s;

$db=$vars["db"];



$total_slot=2;

$one_day_before=date('Y-m-d', ntime()-86400);

$today['start']=date('Y-m-d H:i:s', mktime(0,0,0,ndate('n'),ndate('j'),ndate('Y')));

$today['end']=date('Y-m-d H:i:s', mktime(23,59,59,ndate('n'),ndate('j'),ndate('Y')));

$this_month['start']=date('Y-m-d H:i:s', mktime(0,0,0,ndate('n'),1,ndate('Y')));

$this_month['end']=date('Y-m-d H:i:s', mktime(23,59,59,ndate('n')+1,0,ndate('Y')));



//verification

$head_info=get_user_detail_by_id($hid);

if(!$head_info){

	$break=true;

}

if($get_s["cur_uid"] && $get_s["cur_uid"]!=$hid){

	$head_info=get_user_detail_by_id($get_s["cur_uid"]);

	if(!$head_info){

		$break=true;

	}elseif(!is_group_matrix_downline_of($get_s["cur_uid"], $hid)){

		$break=true;

	}

}

if($break){

	return;

}



//get current head id

if($get_s["cur_uid"]){

	$head_id=$get_s["cur_uid"];

}else{

	$head_id=$hid;

}



//determine starting row

if($get_s["row"] && is_numeric($get_s["row"]) && $get_s["row"]>=1){

	//check query string 1st

	$starting_row=$get_s["row"];

}elseif($get_s["cur_uid"]){

	//check current head change

	$starting_row=1;

}elseif($row){

	//check input row

	$starting_row=$row;

}else{

	//1st time build

	if($highlight_uid){

		$t=@mysql_num_rows($r=mysql_query($sql="select * from $db->users where id='$highlight_uid' and matrix_upline='$head_id' order by matrix_position limit 1"));

		if($t>0){

			$downline=mysql_fetch_assoc($r);

			$starting_row=ceil($downline["matrix_position"] / $total_slot);

		}else{

			$starting_row=1;

		}

	}else{

		$starting_row=1;

	}

}



$starting_row = 1;



$position_start=(($starting_row-1) * $total_slot) + 1;

$position_end=$position_start + $total_slot - 1;

//retrieve downlines within that row

$t=@mysql_num_rows($r=mysql_query($sql="select id from $db->users where matrix_upline='$head_id' and matrix_position>=$position_start and matrix_position<=$position_end"));

for($i=0;$i<$t;$i++){

	$downlinet=mysql_fetch_assoc($r);

	$downline=get_user_detail_by_id($downlinet['id']);

	$downlines[$downline["matrix_position"]]=$downline;

	$downlines[$downline["matrix_position"]]["total_downline"]=get_total_group_downline($downline['id']);

	$downlines[$downline["matrix_position"]]["downline_today"]=get_total_group_downline($downline['id'], $today['start'], $today['end']);

}

//build the slots

$cur_uid_qs=preg_replace('/\&*cur_uid=[0-9]*/', '', $_SERVER["QUERY_STRING"]);

$cur_uid_qs=preg_replace('/\&*row=[0-9]*/', '', $cur_uid_qs);

for($i=0;$i<$total_slot;$i++){

	$this_slot_index = (($starting_row-1) * $total_slot) + ($i+1);

	$did = $downlines[$this_slot_index]["id"];

	$carry_forward = @mysql_result(mysql_query($s="select amount from gg_member_cf where uid='$did' and cdate='$one_day_before'"), 0);

	$ps_today_sales_plana = @mysql_result(mysql_query($s="select sum(s.bv) from gg_purchase a inner join gg_sales_bv s on s.slid=a.id and s.uid='$did' where a.first_sale='y' and a.status='confirmed' and a.cdate>='$today[start]' and a.cdate<='$today[end]'"), 0);

	$pgs_today_sales_plana = calc_group_sales($did, $today['start'], $today['end'], 'matrix_upline', $first_sale = true);

	$ps_month_sales_plana = @mysql_result(mysql_query($s="select sum(s.bv) from gg_purchase a inner join gg_sales_bv s on s.slid=a.id and s.uid='$did' where a.first_sale='y' and a.status='confirmed' and a.cdate>='$this_month[start]' and a.cdate<='$this_month[end]'"), 0);

	$pgs_month_sales_plana = calc_group_sales($did, $this_month['start'], $this_month['end'], 'matrix_upline', $first_sale = true);

	$ps_today_sales_planb = calc_personal_sales($did, $today['start'], $today['end']);

	$pgs_today_sales_planb = calc_personal_sales($did, $today['start'], $today['end'], $first_sale = false);

	$ps_month_sales_planb = calc_personal_sales($did, $this_month['start'], $this_month['end']);

	$pgs_month_sales_planb = calc_personal_sales($did, $this_month['start'], $this_month['end'], $first_sale = false);

	

	$slots.="

	<div class='slot'>

		<div class='slot_connector_".($i==0? "left" : ($i==($total_slot-1)? "right" : "center"))."'></div><div class='clear'></div>

		<div class='slot_line'></div>

		<div class='slot_box".($highlight_uid && $downlines[$this_slot_index]["id"]==$highlight_uid? " slot_highlight" : "")." rank-".str_replace('.', '', $downlines[$this_slot_index]['rank_a'])."'>

			<div class='slot_number'>$this_slot_index</div>".(isset($downlines[$this_slot_index])?

			"<div class='info'>".__('ID').": ".$downlines[$this_slot_index]["id"]."<br />\n".

			$downlines[$this_slot_index]["username"]."<br />\n".

			$downlines[$this_slot_index]["name"]."</div>".

			"<div class='rank'>".__('Rank').": ".($downlines[$this_slot_index]["rank_a"]? $vars['rank_index_a_d'][$downlines[$this_slot_index ]['rank_a']] : "None")."</div>".

			"<div class='plana'><br />

			".__('PS').": ".number_format($ps_today_sales_plana, 2)."(".number_format($ps_month_sales_plana, 2).")"."<br />".

			__('PGS').": ".number_format($pgs_today_sales_plana, 2)."(".number_format($pgs_month_sales_plana, 2).")"."</div>".

			"<div class='carry_forward'>".__('Carry Fwd').": ".number_format($carry_forward, 2).'BV'.'</div>'.

			"<div class='downline'><a href='$vars[this_file]?$cur_uid_qs&cur_uid=".$downlines[$this_slot_index]["id"]."'>".replace_tag(__("<%x%>(<%y%>) <%downline%>"), array("<%x%>"=>strval($downlines[$this_slot_index]["downline_today"]), "<%y%>"=>strval($downlines[$this_slot_index]["total_downline"]), "<%downline%>"=>$downlines[$this_slot_index]["total_downline"]>1? __("downlines") : __("downline")))."</a></div>"

			:

			($enable_radio? "<br />\n<input type='radio' name='slot' ".($selected_slot && $selected_slot=="$head_id-$this_slot_index"? "checked='checked'" : "")." value='$head_id-$this_slot_index' />" : ""))."

		</div>".(isset($downlines[$this_slot_index])? "

		<div class='slot_more'><a href='$vars[this_file]?$cur_uid_qs&cur_uid=".$downlines[$this_slot_index]["id"]."'><img src='".M_URL."/images/network/more.gif' /></a></div>" : "")."

	</div>";

}

$downline_slots="

<div class='downline_slots'>

	$slots

</div>";



//head slot

$cur_uid_qs=preg_replace('/\&*cur_uid=[0-9]*/', '', $_SERVER["QUERY_STRING"]);

$cur_uid_qs=preg_replace('/\&*row=[0-9]*/', '', $cur_uid_qs);

$head_total_downline=get_total_group_downline($head_info['id']);

$head_downline_today=get_total_group_downline($head_info['id'], $today['start'], $today['end']);

$ps_today_sales_plana = @mysql_result(mysql_query($s="select sum(s.bv) from gg_purchase a inner join gg_sales_bv s on s.slid=a.id and s.uid='{$head_info['id']}' where a.first_sale='y' and a.status='confirmed' and a.cdate>='$today[start]' and a.cdate<='$today[end]'"), 0);

$pgs_today_sales_plana = calc_group_sales($head_info['id'], $today['start'], $today['end'], 'matrix_upline', $first_sale = true);

$ps_month_sales_plana = @mysql_result(mysql_query($s="select sum(s.bv) from gg_purchase a inner join gg_sales_bv s on s.slid=a.id and s.uid='{$head_info['id']}' where a.first_sale='y' and a.status='confirmed' and a.cdate>='$this_month[start]' and a.cdate<='$this_month[end]'"), 0);

$pgs_month_sales_plana = calc_group_sales($head_info['id'], $this_month['start'], $this_month['end'], 'matrix_upline', $first_sale = true);

$ps_today_sales_planb = calc_personal_sales($head_info['id'], $today['start'], $today['end']);

$pgs_today_sales_planb = calc_personal_sales($head_info['id'], $today['start'], $today['end'], $first_sale = false);

$ps_month_sales_planb = calc_personal_sales($head_info['id'], $this_month['start'], $this_month['end']);

$pgs_month_sales_planb = calc_personal_sales($head_info['id'], $this_month['start'], $this_month['end'], $first_sale = false);

$head_slot="

<div class='slot_up'>".($get_s["cur_uid"] && $get_s["cur_uid"]!=$hid? "<a href='$vars[this_file]?$cur_uid_qs&cur_uid=$head_info[matrix_upline]'><img src='".M_URL."/images/network/upmore.gif' /></a>" : "")."</div>

	<div class='head_slot'>

	<div class='slot_box".($highlight_uid && $head_info["id"]==$highlight_uid? " slot_highlight" : "")." rank-".str_replace('.', '', $head_info['rank_a'])."'>

		<div class='info'>".__('ID').": "."$head_info[id]<br />\n

		$head_info[username]<br />\n

		$head_info[name]</div>".

		"<div class='rank'>".__('Rank').": ".($head_info["rank_a"]? $vars['rank_index_a_d'][$head_info['rank_a']] : "None")."</div>".

		"<div class='plana'><br />

		".__('PS').": ".number_format($ps_today_sales_plana, 2)."(".number_format($ps_month_sales_plana, 2).")"."<br />".

		__('PGS').": ".number_format($pgs_today_sales_plana, 2)."(".number_format($pgs_month_sales_plana, 2).")"."</div>".

		"<div class='downline'>".replace_tag(__("<%x%>(<%y%>) <%downline%>"), array("<%x%>"=>strval($head_downline_today), "<%y%>"=>strval($head_total_downline), "<%downline%>"=>$head_total_downline>1? __("downlines") : __("downline")))."</div>

	</div>

	<div class='slot_line'></div>

</div>";



//matrix navigation

$nav_qs=preg_replace('/\&*row=[0-9]*/', '', $_SERVER["QUERY_STRING"]);

if($starting_row>1){

	$prev_nav="<a href='$vars[this_file]?$nav_qs&row=".($starting_row-1)."'>Prev</a>";

}

$prev_nav="<div class='slot_nav'>$prev_nav</div>";

$next_nav="<div class='slot_nav'><a href='$vars[this_file]?$nav_qs&row=".($starting_row+1)."'>Next</a></div>";

$prev_nav="<div class='slot_nav'></div>";

$next_nav="<div class='slot_nav'></div>";

if(is_group_matrix_downline_of($get_s["cur_uid"], $uid)){

	$up_nav="<a href='$vars[this_file]?uid=$uid&cur_uid=$head_info[matrix_upline]'>Up</a>";

}

$up_nav="<div class='slot_up'>$up_nav</div>";



//whole matrix

$matrix="

<input type='hidden' name='upline' value=\"$head_id\" />

<div class='".$css_prefix."_matrix'> 

	<div class='matrix_body'>

		$up_nav

		$head_slot

		$downline_slots

	</div><div class='clear'></div>


</div><div class='clear'></div>";



return $matrix;

}

?>